package ui.views;

import java.io.File;

import net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput;

import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ui.provider.SqlFileContent;
import ui.provider.SqlFileLabel;
import ui.util.IPartId;

public class SqlFileView extends ViewPart {
	public final static Logger logger = LoggerFactory
			.getLogger(SqlFileView.class);
	// public static final String ID = "filebrowser.view";
	private TreeViewer viewer;
	private String pathname;

	public void createPartControl(Composite parent) {
		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.setContentProvider(new SqlFileContent());
		viewer.setLabelProvider(new SqlFileLabel());

		pathname = IPartId.sqlReport;
		viewer.setInput(new File[] { new File(pathname) });
		getSite().setSelectionProvider(viewer);

//		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
//			public void selectionChanged(SelectionChangedEvent event) {
//				// if the selection is empty clear the label
//				logger.debug("Test Open View1");
//				if (event.getSelection().isEmpty()) {
//					// text.setText("");
//					return;
//				}
//				if (event.getSelection() instanceof IStructuredSelection) {
//					// IViewPart viewPart;
//					// String viewId = "com.quantum.view.sqlqueryview";
//					// String secondaryId = "1";
//					// logger.debug("Test Open View");
//					//
//					// try {
//					// viewPart = PlatformUI.getWorkbench()
//					// .getActiveWorkbenchWindow().getActivePage()
//					// .showView(viewId, secondaryId, 3);
//					// } catch (PartInitException e) {
//					// // TODO Auto-generated catch block
//					// e.printStackTrace();
//					// }
//					// IStructuredSelection selection =
//					// (IStructuredSelection)event.getSelection();
//					// StringBuffer toShow = new StringBuffer();
//					// for (Iterator iterator = selection.iterator();
//					// iterator.hasNext();) {
//					// Object domain = (Model) iterator.next();
//					// String value = labelProvider.getText(domain);
//					// toShow.append(value);
//					// toShow.append(", ");
//					// }
//					// // remove the trailing comma space pair
//					// if(toShow.length() > 0) {
//					// toShow.setLength(toShow.length() - 2);
//					// }
//					// text.setText(toShow.toString());
//				}
//			}
//		});
//
//	}

	 viewer.addOpenListener(new IOpenListener() {
		 @Override
		 public void open(OpenEvent event) {
			 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
	
			 File file = (File) selection.getFirstElement();
			 SQLEditorInput editorInput = new SQLEditorInput(file);
			 IWorkbenchPage page =
				 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			 try {
				 page.openEditor(editorInput,IPartId.SqlEditor);
			 } catch (Exception e) {
				 System.out.println("Fail to Open Editor" + e.toString());
			 }
		 }
	 	});
	}
	// if (Desktop.isDesktopSupported()) {
	// Desktop desktop = Desktop.getDesktop();
	// if (desktop.isSupported(Desktop.Action.OPEN)) {
	// try {
	// desktop.open(file);
	// } catch (IOException e) {
	// // DO NOTHING
	// }
	// }
	// }
	// }
	// });
	// }

	public void setFocus() {
		viewer.getControl().setFocus();
	}

}
